﻿using System.Collections.Generic;
using System.Threading.Tasks;
namespace Odyssey.Consul.Implementations
{
    using Features;
    using Http;
    using Models;
    public class Config : BaseConsulAPI, IConfig
    {
        public Config(IHttpRequester httpRequester, IConsulConfiguration consulConfiguration) : base(httpRequester, consulConfiguration, EnumResource.Agent)
        {

        }

        private string BuildConfigPath(string action = "")
        {
            return BuildUri("config", action);
        }


        public async Task ApplyConfiguration(ArguConfig arguConfig)
        {
            var data = new
            {
                arguConfig.Kind,
                arguConfig.Name,
                arguConfig.Protocol
            };
            var queryParameter = new QueryParameter();
            if (!string.IsNullOrWhiteSpace(arguConfig.DC))
            {
                queryParameter.Datacenter = arguConfig.DC;
            }
            if (arguConfig.Cas.HasValue)
            {
                queryParameter.OtherParameters.Add("cas", arguConfig.Cas.ToString());
            }

            await ApiSendResponse(BuildConfigPath(), data, EnumConsulHttpMethod.Put, queryParameter, BuildTokenHeader(new EnumResource[] {
                EnumResource.Service,
                EnumResource.Operator
            }));
        }

        public async Task DeleteConfiguration(ArguConfig arguConfig)
        {
            var path = BuildConfigPath($"{arguConfig.Kind}/{arguConfig.Name}");
            QueryParameter queryParameter = null;
            if (!string.IsNullOrWhiteSpace(arguConfig.DC))
            {
                queryParameter = new QueryParameter
                {
                    Datacenter = arguConfig.DC
                };
            }
            await ApiSendResponse(path, queryParameter: queryParameter);
        }

        public async Task<ResultConfigGetConfiguration> GetConfiguration(ArguConfig arguConfig)
        {
            var path = BuildConfigPath($"{arguConfig.Kind}/{arguConfig.Name}");
            QueryParameter queryParameter = null;
            if (!string.IsNullOrWhiteSpace(arguConfig.DC))
            {
                queryParameter = new QueryParameter
                {
                    Datacenter = arguConfig.DC
                };
            }
            return await ApiSend<ResultConfigGetConfiguration>(path, EnumConsulHttpMethod.Get, queryParameter);
        }

        public async Task<List<ResultConfigGetConfiguration>> ListConfiguration(ArguConfig arguConfig)
        {
            QueryParameter queryParameter = null;
            if (!string.IsNullOrWhiteSpace(arguConfig.DC))
            {
                queryParameter = new QueryParameter
                {
                    Datacenter = arguConfig.DC
                };
            }
            return await ApiSend<List<ResultConfigGetConfiguration>>(BuildConfigPath(arguConfig.Kind), EnumConsulHttpMethod.Get, queryParameter);
        }
    }
}
